<template>
    <div class="login web-inner">
        <div class="app-top">
            <span>會員登錄</span>
            <span>|</span>
            <span><a style="color: #ffffff" href="#/kandeng">品牌刊登</a></span>
            <span>|</span>
            <span><a style="color: #ffffff" href="#/register">加入會員</a></span>
        </div>
        <div class="login-main">
            <a href="#/index">
                <img src="../../static/img/logo3.png" alt="">
            </a>
            <div class="login-box">
                <div class="title">會員登錄</div>
                <div class="inner">
                    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
                        <el-form-item label="帳號" prop="account">
                            <el-input v-model.number="ruleForm.account"></el-input>
                        </el-form-item>
                        <el-form-item label="密码" prop="pwd">
                            <el-input type="password" v-model="ruleForm.pwd" autocomplete="off"></el-input>
                        </el-form-item>
                        <div class="other">
                            <div class="left">
                                <div>
                                    <el-checkbox v-model="ruleForm.remember">记住密码</el-checkbox>
                                </div>
                                <div>
                                    <a href="">忘記密碼</a> | <a href="#/register">加入會員</a>
                                </div>
                                <div></div>
                            </div>
                            <div class="right">
                                <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
                            </div>
                        </div>
                    </el-form>
                </div>
            </div>
        </div>
        <commonBottom :menu="menu" class="bottom"></commonBottom>
    </div>
</template>

<script>
    import qs from 'qs';
    import commonBottom from "../components/common-bottom"
    export default {
        name: "login",
        components:{
            commonBottom
        },
        data() {
            var checkAccount = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('請輸入賬號！'));
                } else {
                    callback();
                }
            };
            var checkPwd = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('請輸入密碼！'));
                } else {
                    callback();
                }
            };
            return {
                menu:[
                    {
                        title:"首頁",
                        path:"/"
                    },
                    {
                        title:"24小時加盟展",
                        path:"/jiamengzhan"
                    },
                ],
                ruleForm: {
                    account: '',
                    pwd: '',
                    remember:false
                },
                rules:{
                    account:[
                        { validator: checkAccount, trigger: 'blur' }
                    ],
                    pwd:[
                        { validator: checkPwd, trigger: 'blur' }
                    ]
                }
            }
        },
        mounted() {
            sessionStorage.setItem("kaidian+login","")
            this.getBrandCategory()
            this.getUser();
        },
        methods:{
            getUser() {
                let account = localStorage.getItem("kaidian+account");
                let pwd = localStorage.getItem("kaidian+pwd");
                this.ruleForm.account=account?account:"";
                this.ruleForm.pwd=pwd?pwd:"";
            },
            getBrandCategory() {
                this.$http.post(this.API.get_brand_category).then((res) => {
                    let $this = this;
                if(res.status ===200 && res.data.length>0){
                    res.data.forEach((item) => {
                        item.path = "/pinpai/" + item.order
                    this.menu.push(item)
                })
                }
            })
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.loginData();
                    } else {
                        console.log('error submit!!');
                return false;
            }
            });
            },
            safaUser() {
                sessionStorage.setItem("kaidian+login",this.ruleForm.account)
                if(this.ruleForm.remember){
                    localStorage.setItem("kaidian+account",this.ruleForm.account)
                    localStorage.setItem("kaidian+pwd",this.ruleForm.pwd)
                }
            },
            loginData() {
                let data = {
                    account:this.ruleForm.account,
                    pwd:this.ruleForm.pwd
                }
                this.$http({
                    method:"post",
                    url:this.API.user_login,
                    data:qs.stringify(data),
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    withCredentials:true
                }).then((res) => {
                    if(res.status ===200 && res.data[0].code==="101"){
                        this.safaUser();
                        this.$message({
                            message: '登陸成功',
                            type: 'success'
                        });
                        location.href="#/index"
                    }else{
                        this.$message.error('登陸失敗');
                    }
                })
            },
        }
    }
</script>

<style scoped lang="less">
    @import "../style/base";
    .login{
        margin: 0 auto;
        height: 100%;
        /*position: relative;*/
        min-height: 100%;
    }
    .app-top{
        height:50px;
        width: 100%;
        line-height: 50px;
        background-color: #333333;
        text-align: right;
        color: #ffffff;
        font-size: 16px;
        span{
            margin-left: 5px;
            margin-right: 5px;
            &:last-child{
                margin-right: 25px;
            }
            &:nth-child(2n-1){
                cursor: pointer;
                &:hover{
                    color: gray;
                }
            }
        }
    }
    .bottom{
        /*position: absolute;*/
        /*bottom: 0;*/
        /*left: 0;*/

    }
    .login-main{
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        width: 450px;
        margin:100px  auto;
        img{
            width: 350px;
            margin-bottom: 30px;
        }
        .login-box{
            width: 100%;
            background-color: #ffffff;
            .title{
                height: 50px;
                line-height: 50px;
                background-color: #e6e6e6;
                text-align: right;
                padding-right: 20px;
                box-sizing: border-box;
                font-size: 20px;
                color: #000000;
                font-weight: bold;
            }
            .inner{
                padding: 50px;
                box-sizing: border-box;
            }
            .other{
                font-size: 14px;
                line-height: 22px;
                display: flex;
                flex-direction: row;
                justify-content: space-between;
                a{
                    color: #606266;
                }
            }
        }
    }

</style>
<style lang="less">
    .login{
        .el-form-item__label{
            float: none;
            font-size: 16px;
            color: #000000;
        }
    }
</style>